package com.gdd.videos.mini_api.controller;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.gdd.videos.common.common.GddResult;
import com.gdd.videos.mini_api.BasicController;
import com.gdd.videos.pojo.User;
import com.gdd.videos.service.UserService;
import com.gdd.videos.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName parent
 * @title     UserController   
 * @package    com.gdd.videos.mini_api.controller  
 * @date   2019/9/5 14:21  
 * @explain
 */
@Api(value = "用户相关的业务接口", tags = "微信端用户相关的Controller")
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController extends BasicController{


    @Autowired
    private UserService userService;


    /**
     * 上传用户头像
     *
     * @param userId
     * @param files
     * @return
     */
    @ApiOperation(value = "上传头像", notes = "用户上传头像的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "上传头像的用户id", required = true,
                    dataType = "String", paramType = "query")
    })
    @PostMapping("/uploadFace")
    public GddResult uploadFace(String userId, @RequestParam("file") MultipartFile[] files) throws Exception {
        System.out.println(fileSpace);

        if (StringUtils.isEmpty(userId)){
            return GddResult.errorMsg("请先登录");
        }

        FileOutputStream fos = null;
        InputStream ins = null;
        // 上传头像保存到数据库中的相对路径
        String filePathDB = "/" + userId + "/face";
        try {
            if (files != null && files.length > 0) {
                // 判断文件名称是否为空
                String filename = files[0].getOriginalFilename();
                if (StringUtils.isNotEmpty(filename)) {
                    // 文件最终上传保存的路径
                    String finalFacePath = fileSpace + filePathDB + "/" + filename;

                    // 数据库保存路径
                    filePathDB += ("/" + filename);

                    // 判断目录是否存在,不存在则创建目录,存在则直接存储
                    File outFile = new File(finalFacePath);
                    if (outFile.getParentFile() == null || !outFile.getParentFile().isDirectory()) {
                        // 创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }

                    // 上传文件(使用apache的IOUtils工具类)
                    fos = new FileOutputStream(outFile);
                    ins = files[0].getInputStream();
                    IOUtils.copy(ins, fos);
                }
            }else {
                return GddResult.errorMsg("上传失败...");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return GddResult.errorMsg("上传失败...");
        } finally {
            // 释放资源
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            if (ins != null) {
                ins.close();
            }
        }

        // 保存数据库用户头像信息
        User user = new User();
        user.setId(userId);
        user.setFaceImage(filePathDB);
        userService.updateUserInfo(user);

        return GddResult.ok(filePathDB);
    }


    @ApiOperation(value = "根据id获取用户信息",notes = "获取用户信息的Controller")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,
            dataType = "String",paramType = "query")
    @PostMapping("/queryUserInfo")
    public GddResult queryUserInfo(String userId){

        if (StringUtils.isEmpty(userId)){
            return GddResult.errorMsg("用户id不能为空...");
        }

        // 查找用户信息
        User user = userService.findByUserId(userId);

        UserVo userVo = new UserVo();

        BeanUtils.copyProperties(user,userVo);

        return GddResult.ok(userVo);
    }



}
